package com.it.whitejotterapi.service.impl;

import com.it.whitejotterapi.dao.AdminUserDao;
import com.it.whitejotterapi.entity.AdminUser;
import com.it.whitejotterapi.service.AdminUserService;
import com.it.whitejotterapi.tools.JsonResult;
import com.it.whitejotterapi.tools.LogTools;
import com.it.whitejotterapi.tools.Md5Tools;
import com.it.whitejotterapi.tools.TokenTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 羡羡
 * @Date: 2022/05/23/08:56
 */
@Service
public class AdminUserServiceImpl implements AdminUserService {

    @Autowired
    AdminUserDao adusdao;

    /**
     * 用户登入
     * @param username 账号
     * @param password 密码
     * @return
     */
    @Override
    public JsonResult login(String username, String password) throws NoSuchAlgorithmException {
        //封装数据
        Map mlo = new HashMap();
        mlo.put("username",username);
        mlo.put("password",password);
        //输出日志
        LogTools.printlog("login","POST",mlo);
        AdminUser aduser=adusdao.login(username, Md5Tools.getmdresult(password));
        //账号或密码错误！
        if(aduser==null){
            return new JsonResult(201,"账号或密码错误！");
        }else{
            //登入成功
            Map mp=new HashMap();
            mp.put("id",aduser.id);
            mp.put("username",aduser.username);
            mp.put("name",aduser.name);
            mp.put("phone",aduser.phone);
            mp.put("email",aduser.email);
            mp.put("enabled",aduser.enabled);
            mp.put("isDelete",aduser.isDelete);
            mp.put("identity",aduser.identity);
            //TokenTools.tokencreate(aduser) token生成
            mp.put("token",TokenTools.tokencreate(aduser));
            return new JsonResult(200,"登入成功",mp);
        }
    }

    /**
     * 用户注册
     * @param adus 用户实体
     * @return
     */
    @Override
    public JsonResult registeruser(AdminUser adus) {
        LogTools.printlog("registerus","POST",adus);
        int regisnumber=adusdao.registeruser(adus);
        if(regisnumber==1){
            return new JsonResult(200,"注册成功！");
        }else{
            return new JsonResult(201,"注册失败！");
        }
    }
}
